import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5 import uic
from core.BaslerCommunication import BaslerCam
import numpy as np
import pyqtgraph as pg

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.bcamera = None
        self.ui = uic.loadUi('./testExample.ui', self)
        self.initCam()
        self.setupCamList()
        self.pushButton_open.setEnabled(False)
        self.comboBox.activated.connect(self.pushButtonEnable)
        self.pushButton_open.clicked.connect(self.openCam)

    def initCam(self):
        try:
            self.cam = BaslerCam()
            print('初始化成功')
        except:
            pass

    def setupCamList(self):
        try:
            self.comboBox.clear()

            CamList = self.cam.returnCamList()
            print('添加列表成功')
            for cam in CamList:
                self.comboBox.addItem(cam)

        except:
            pass

    def pushButtonEnable(self):
        self.pushButton_open.setEnabled(True)

    def getCurrentCam(self):
        return {0: str(self.comboBox.currentText())}

    def openCam(self):
        if self.bcamera:
            self.cam.close()
        self.cam.openCommunications(self.getCurrentCam())


if __name__ == '__main__':
    app = QApplication(sys.argv)
    # #print(QStyleFactory.keys())
    app.setStyle('Fusion')
    wnd = MainWindow()
    wnd.setGeometry(
        QStyle.alignedRect(
            Qt.LeftToRight,
            Qt.AlignCenter,
            wnd.size(),
            app.desktop().availableGeometry()
        )
    )
    print('结尾')
    wnd.show()
    sys.exit(app.exec_())
